package com.linx.workflow.service.impl;

import cn.hutool.core.io.IORuntimeException;
import cn.hutool.core.io.IoUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.linx.common.core.utils.StringUtils;
import com.linx.common.mybatis.core.page.PageQuery;
import com.linx.common.mybatis.core.page.TableDataInfo;
import com.linx.common.tenant.helper.TenantHelper;
import com.linx.workflow.domain.WfDeployForm;
import com.linx.workflow.domain.bo.ProcessQuery;
import com.linx.workflow.domain.vo.WfDeployVo;
import com.linx.workflow.mapper.WfCategoryMapper;
import com.linx.workflow.mapper.WfDeployFormMapper;
import com.linx.workflow.service.IWfDefinitionConfigService;
import com.linx.workflow.service.IWfDeployService;
import com.linx.workflow.utils.ProcessUtils;
import lombok.RequiredArgsConstructor;
import org.flowable.common.engine.impl.db.SuspensionState;
import org.flowable.engine.RepositoryService;
import org.flowable.engine.repository.Deployment;
import org.flowable.engine.repository.ProcessDefinition;
import org.flowable.engine.repository.ProcessDefinitionQuery;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author nbacheng
 * @createTime 2022/6/30 9:04
 */
@RequiredArgsConstructor
@Service
public class WfDeployServiceImpl implements IWfDeployService {

    private final RepositoryService repositoryService;
    private final WfDeployFormMapper deployFormMapper;
    private final WfCategoryMapper categoryMapper;
    private final IWfDefinitionConfigService wfDefinitionConfigService;

    @Override
    public TableDataInfo<WfDeployVo> queryPageList(ProcessQuery processQuery, PageQuery pageQuery) {
        // 获取流程定义列表
        ProcessDefinitionQuery processDefinitionQuery = repositoryService.createProcessDefinitionQuery()
        	.processDefinitionTenantId(TenantHelper.getTenantId())
            .latestVersion()
            .orderByProcessDefinitionKey()
            .asc();
        // 构建搜索条件
        ProcessUtils.buildProcessSearch(processDefinitionQuery, processQuery);
        long pageTotal = processDefinitionQuery.count();
        if (pageTotal <= 0) {
            return TableDataInfo.build();
        }
        int offset = pageQuery.getPageSize() * (pageQuery.getPageNum() - 1);
        List<ProcessDefinition> definitionList = processDefinitionQuery.listPage(offset, pageQuery.getPageSize());

        List<WfDeployVo> deployVoList = new ArrayList<>(definitionList.size());
        for (ProcessDefinition processDefinition : definitionList) {
        	if(StringUtils.isNotBlank(processQuery.getAppType())) {
        		if ( processQuery.getAppType().equalsIgnoreCase(categoryMapper.selectAppTypeByCode(processDefinition.getCategory(),TenantHelper.getTenantId()))) {
		            Deployment deployment = repositoryService.createDeploymentQuery().deploymentId(processDefinition.getDeploymentId()).singleResult();
		            WfDeployVo wfDeployVo = new WfDeployVo();
		            wfDeployVo.setDefinitionId(processDefinition.getId());
		            wfDeployVo.setProcessKey(processDefinition.getKey());
		            wfDeployVo.setProcessName(processDefinition.getName());
		            wfDeployVo.setVersion(processDefinition.getVersion());
		            wfDeployVo.setCategory(processDefinition.getCategory());
		            wfDeployVo.setAppType(categoryMapper.selectAppTypeByCode(processDefinition.getCategory(),TenantHelper.getTenantId()));
		            wfDeployVo.setDeploymentId(processDefinition.getDeploymentId());
		            wfDeployVo.setSuspended(processDefinition.isSuspended());
		            wfDeployVo.setCategory(deployment.getCategory());
		            wfDeployVo.setDeploymentTime(deployment.getDeploymentTime());
		            deployVoList.add(wfDeployVo);
        		}
        	}
        	else {
	            String deploymentId = processDefinition.getDeploymentId();
	            Deployment deployment = repositoryService.createDeploymentQuery().deploymentId(deploymentId).singleResult();
	            WfDeployVo wfDeployVo = new WfDeployVo();
	            wfDeployVo.setDefinitionId(processDefinition.getId());
	            wfDeployVo.setProcessKey(processDefinition.getKey());
	            wfDeployVo.setProcessName(processDefinition.getName());
	            wfDeployVo.setVersion(processDefinition.getVersion());
	            wfDeployVo.setCategory(processDefinition.getCategory());
	            wfDeployVo.setAppType(categoryMapper.selectAppTypeByCode(processDefinition.getCategory(),TenantHelper.getTenantId()));
	            wfDeployVo.setDeploymentId(processDefinition.getDeploymentId());
	            wfDeployVo.setSuspended(processDefinition.isSuspended());
	            wfDeployVo.setCategory(deployment.getCategory());
	            wfDeployVo.setDeploymentTime(deployment.getDeploymentTime());
	            deployVoList.add(wfDeployVo);
        	}
        }
        Page<WfDeployVo> page = new Page<>();
        page.setRecords(deployVoList);
        page.setTotal(pageTotal);
        return TableDataInfo.build(page);
    }

    @Override
    public TableDataInfo<WfDeployVo> queryPublishList(String processKey, PageQuery pageQuery) {
        // 创建查询条件
        ProcessDefinitionQuery processDefinitionQuery = repositoryService.createProcessDefinitionQuery()
        	.processDefinitionTenantId(TenantHelper.getTenantId())
            .processDefinitionKey(processKey)
            .orderByProcessDefinitionVersion()
            .desc();
        long pageTotal = processDefinitionQuery.count();
        if (pageTotal <= 0) {
            return TableDataInfo.build();
        }
        // 根据查询条件，查询所有版本
        int offset = pageQuery.getPageSize() * (pageQuery.getPageNum() - 1);
        List<ProcessDefinition> processDefinitionList = processDefinitionQuery
            .listPage(offset, pageQuery.getPageSize());
        List<WfDeployVo> deployVoList = processDefinitionList.stream().map(item -> {
            WfDeployVo vo = new WfDeployVo();
            vo.setDefinitionId(item.getId());
            vo.setProcessKey(item.getKey());
            vo.setProcessName(item.getName());
            vo.setVersion(item.getVersion());
            vo.setCategory(item.getCategory());
            vo.setDeploymentId(item.getDeploymentId());
            vo.setSuspended(item.isSuspended());
            return vo;
        }).collect(Collectors.toList());
        Page<WfDeployVo> page = new Page<>();
        page.setRecords(deployVoList);
        page.setTotal(pageTotal);
        return TableDataInfo.build(page);
    }

    /**
     * 激活或挂起流程
     *
     * @param state 状态
     * @param definitionId 流程定义ID
     */
    @Override
    public void updateState(String definitionId, String state) {
        if (SuspensionState.ACTIVE.toString().equals(state)) {
            // 激活
            repositoryService.activateProcessDefinitionById(definitionId, true, null);
        } else if (SuspensionState.SUSPENDED.toString().equals(state)) {
            // 挂起
            repositoryService.suspendProcessDefinitionById(definitionId, true, null);
        }
    }

    @Override
    public String queryBpmnXmlById(String definitionId) {
        InputStream inputStream = repositoryService.getProcessModel(definitionId);
        try {
            return IoUtil.readUtf8(inputStream);
        } catch (IORuntimeException exception) {
            throw new RuntimeException("加载xml文件异常");
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteByIds(List<String> deployIds) {
        for (String deployId : deployIds) {
            repositoryService.deleteDeployment(deployId, true);
            deployFormMapper.delete(new LambdaQueryWrapper<WfDeployForm>().eq(WfDeployForm::getDeployId, deployId));
        }
    }

    /**
     * 根据key获取最新版本的流程定义
     */
    @Override
    public ProcessDefinition getProcessDefinitionByKey(String processKey) {
        ProcessDefinitionQuery processDefinitionQuery = repositoryService.createProcessDefinitionQuery()
            .processDefinitionTenantId(TenantHelper.getTenantId())
            .latestVersion()
            .processDefinitionKey(processKey);

        return processDefinitionQuery.singleResult();
    }
}
